package nbpMaven2.nbpMaven2;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CurrencyPostgres {

	/************************************/
	/************* FUNKCJE ****************/
	/************************************/

	// przypisuje do krajow waluty
	public void setCurrencyToCountry(String currShort, String country) {
		try {
			new DBConnectionClass().setCurrencyToCountry(currShort, country);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SomeException("There is Some Exception!");
		}
	}

	// podaje maksymalna wartosc kursow walut z danego dnia
	public CurrencyInfoClass getMaxCurrValue(Date date) {
		CurrencyInfoClass cic = null;
		DBConnectionClass dbconn = new DBConnectionClass();
		try {

			if (dbconn.checkDateInAvgTab(date)) {
				cic = dbconn.getCurrencyShortcutAndVal(date, "MAX");
				// System.out.println("DB");
			}

			else {
				XMLReaderClass xmlReaderObj = new XMLReaderClass();
				String nazwa = xmlReaderObj.getXmlFile(date);
				// System.out.println(nazwa + "    XML");
				cic = xmlReaderObj.getSomeValue(nazwa, "MAX");
				// cic =
				// xmlReaderObj.getSomeValue(xmlReaderObj.getXmlFile(date),
				// "MAX");
			}

		} catch (ClassNotFoundException ex) {
			throw new RuntimeException("This class is not exists!");
		} catch (SQLException e) {
			throw new SqlExcept("There is some SQL Exception!");
		}

		return cic;
	}

	// podaje minimalna wartosc kursow walut z danego dnia
	public CurrencyInfoClass getMinCurrValue(Date date) {
		CurrencyInfoClass cic = null;
		DBConnectionClass dbconn = new DBConnectionClass();

		try {

			if (dbconn.checkDateInAvgTab(date))
				cic = dbconn.getCurrencyShortcutAndVal(date, "MIN");

			else {
				XMLReaderClass xmlReaderObj = new XMLReaderClass();
				cic = xmlReaderObj.getSomeValue(xmlReaderObj.getXmlFile(date), "MIN");
			}

		} catch (ClassNotFoundException ex) {
			throw new ClassIsNotExist("This class is not exists!");
		} catch (SQLException e) {
			throw new SqlExcept("There is some SQL Exception!");
		}
		return cic;
	}

	// podaje info o krajach z wi�cej niz 1 walutva
	public List<CurrencyInfoClass> searchForMoreCurrency() {
		List<CurrencyInfoClass> info = new ArrayList<CurrencyInfoClass>();
		try {
			// wyszukiwanie kraju z wiecej niz 1 waluta

			// int value = 0;
			Connection conn = new DBConnectionClass().createConnection();
			Statement state = conn.createStatement();
			ResultSet res = state.executeQuery("select countryname, counter from (select countryname, count(*) as counter from country where countryname in (select distinct countryname from country) group by countryname) as tab where counter > 1");
			while (res.next()) {

				info.add(new CurrencyInfoClass(res.getString("countryname"), null));

			}
			res.close();
			conn.close();
			conn.close();
		} catch (ClassNotFoundException ex) {
			throw new ClassIsNotExist("This class is not exists!");
		}

		catch (SQLException ex) {
			throw new SqlExcept("There is some SQL Exception!");
		}
		return info;
	}

	// podaje min i max z danego dnia

	public List<CurrencyInfoClass> getMinMaxCurrValue(Date date1, Date date2) throws DateIsNotCorrect, NoRateThisDay {
		// podaje min i max wszystkich walut z podanego przedzialu

		List<CurrencyInfoClass> obj = new ArrayList<CurrencyInfoClass>();
		try {
			// System.out.println(date1+" "+date2);
			long diff = date1.getTime() - date2.getTime();
			long diffByDay = diff / (24 * 60 * 60 * 1000);
			Date finalDate;
			if (diffByDay < 0) {
				diffByDay = diffByDay * (-1);
				finalDate = date1;
			} else
				finalDate = date2;

			Calendar cal = Calendar.getInstance();
			List<CurrencyInfoClass> myList = new ArrayList<CurrencyInfoClass>();
			DBConnectionClass dbconn = new DBConnectionClass();
			String fName = "";

			for (int i = 0; i <= diffByDay; i++) {
				cal.setTime(finalDate);
				cal.add(Calendar.DATE, i);
				// BigDecimal testbd = null;
				CurrencyInfoClass cic = null;
				// System.out.println("Date[" + i + "] = " + cal.getTime());
				if (dbconn.checkDateInAvgTab(cal.getTime())) {
					System.out.println(cal.getTime() + "    DB");
					cic = dbconn.getCurrencyShortcutAndVal(cal.getTime(), "MAX");
					myList.add(cic);
					cic = dbconn.getCurrencyShortcutAndVal(cal.getTime(), "MIN");
					myList.add(cic);
				} else {
					System.out.println(cal.getTime() + "     XML");
					XMLReaderClass xmlReaderObj = new XMLReaderClass();
					fName = xmlReaderObj.getXmlFile(cal.getTime());
					// System.out.println(fName);
					cic = xmlReaderObj.getSomeValue(fName, "MAX");
					if (cic != null) {
						myList.add(cic);
						myList.add(xmlReaderObj.getSomeValue(xmlReaderObj.getXmlFile(cal.getTime()), "MIN"));
					}
				}
			}
			BigDecimal maxBdVar = BigDecimal.ZERO;
			String maxStrVar = "";
			BigDecimal minBdVar = BigDecimal.ZERO;
			String minStrVar = "";
			for (CurrencyInfoClass k : myList) {
				// wyszkukiwanie min i max na liscie objektow
				// System.out.println(k.getShortcut() + " - " + k.getBd());
				if (k.getShortcut() != null) {
					if (k.getBd().compareTo(maxBdVar) == 1) {
						maxBdVar = k.getBd();
						maxStrVar = k.getShortcut();
					}

					if (k.getBd().compareTo(maxBdVar) == -1) {
						minBdVar = k.getBd();
						minStrVar = k.getShortcut();
					}
				}
			}
			// System.out.println(maxStrVar + " - " + maxBdVar);
			obj.add(new CurrencyInfoClass(maxStrVar, maxBdVar));
			obj.add(new CurrencyInfoClass(minStrVar, minBdVar));
		} catch (NullPointerException ex) {
			throw new NullPointExcept("There is null pointer exception!");
		} catch (ClassNotFoundException ex) {
			throw new ClassIsNotExist("This class is not exists!");
		} catch (SQLException e) {
			throw new SqlExcept("There is some SQL Exception!");
		}

		return obj;
	}

	// podaje max ro�nice z danego przedzialu uwzgledniajac wszystkie waluty
	public String getBiggestDifference(Date date1, Date date2) throws DateIsNotCorrect, NoRateThisDay {
		String currName = "";
		try {
			long diff = date1.getTime() - date2.getTime();
			long diffByDay = diff / (24 * 60 * 60 * 1000);
			Date finalDate;
			if (diffByDay < 0) {
				diffByDay = diffByDay * (-1);
				finalDate = date1;
			} else
				finalDate = date2;
			// System.out.println(diffByDay);
			Calendar cal = Calendar.getInstance();
			DBConnectionClass dbconn = new DBConnectionClass();

			for (int i = 0; i <= diffByDay; i++) {
				cal.setTime(finalDate);
				cal.add(Calendar.DATE, i);
				// System.out.println("Date[" + i + "] = " + cal.getTime());

				if (!dbconn.checkDateInAvgTab(cal.getTime())) {
					XMLReaderClass xmlReaderObj = new XMLReaderClass();
					String fileName = "";
					fileName = xmlReaderObj.getXmlFile(cal.getTime());
					xmlReaderObj.getValueWithCountry(fileName, cal.getTime());
				}

			}
			// teraz wyszukiwanie w bazie i obliczenia
			currName = dbconn.getBiggestCurrencyNameDifference();
		}

		catch (NullPointerException ex) {
			ex.printStackTrace();
		}

		catch (ClassNotFoundException ex) {
			throw new ClassIsNotExist("This class is not exists!");
		}

		catch (SQLException ex) {
			throw new SqlExcept("There is some SQL Exception!");
		}

		return currName;
	}

	/*****************************************************************/

}
